<?php
class Config {
    public $debug;

	public $dbhost;
	public $dbuser;
	public $dbpass;
	public $dbname;

	public $hostScheme;
	public $hostUrl;
	public $hostContext;
	public $editPath;
	public $jsPaths;
	public $cssPaths;

    private function init() {
        $this->debug = false;

        $this->dbhost = '';
        $this->dbuser = '';
        $this->dbpass = '';
        $this->dbname = '';

        $this->hostScheme = 'http';
        $this->hostUrl = '';
        $this->hostContext = '';
        $this->editPath = '/edit/';
		$this->jsPaths = array();
		$this->cssPaths = array();
    }

	public function __construct() {
		$this->init();
	}

    /**
     * Get the local host url
     * @return string
     */
    public function getLocalhostUrl() {
		if (!$this->hostScheme) {
			$this->hostScheme = 'http';
		}

		if (!$this->hostUrl) {
			$this->hostUrl = 'localhost';
		}

		return $this->hostScheme.'://'.$this->hostUrl.'/'.$this->hostContext;
	}

    /**
     * Get the local host full url, it contains the parameters passed by client.
     * For example: http://localhost/demo/post?x=1&y=2
     * @return string
     */
    function getCurrentFullUrl() {
        $server_name = $_SERVER['SERVER_NAME'];
        $port = $_SERVER['SERVER_PORT'];
        $uri = $_SERVER['REQUEST_URI'];
        $full_url = $this->hostScheme. '://'.$server_name;
        if ($port != 80) {
            $full_url .= ':'.$port;
        }
        $full_url .= $uri;
        return $full_url;
    }

    /**
     * @return string
     */
    public function getLocalEditUrl() {
        $hostUrl = $this->getLocalhostUrl();
        return $hostUrl.$this->editPath;
    }

    public function getEditUrlHost($url) {
        $localEditUrl = $this->getLocalEditUrl();
        $index = strpos($url,$localEditUrl);
        if ($index === 0) {
            $editUrl = substr($url, strlen($localEditUrl));
            $parsed = parse_url($editUrl);
            $scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'http';
            $host = $parsed['host'];
            $port = isset($parsed['port']) ? $parsed['port'] : '';
            $hostUrl = $scheme.'://'.$host;
            if ($port) {
                $hostUrl .= ':'.$port;
            } else {
                $hostUrl .= '/';
            }
            return $hostUrl;
        }
        return false;
    }
}